﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace Microsoft.Telco.Controls.WindowsLive
{
    /// <summary>
    /// Control that displays a list of contacts.
    /// </summary>
    #region Parts and States Model Declarations
    [TemplatePart(Name = "ContactsList", Type = typeof(ItemsControl))]
    [TemplatePart(Name = "MainScrollViewer", Type = typeof(ScrollViewer))]
    #endregion
    public class WLContactsControl : Control
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WLContactsControl"/> class.
        /// </summary>
        public WLContactsControl()
        {
            this.DefaultStyleKey = typeof(WLContactsControl);
        }

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) call <see cref="M:System.Windows.Controls.Control.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ContactsList = GetTemplateChild("ContactsList") as ItemsControl;
            MainScrollViewerElement = GetTemplateChild("MainScrollViewer") as ScrollViewer;
            if (MainScrollViewerElement != null)
            {
                SetScrolViewerHeight();
            }
        }

        #region parts
        /// <summary>
        /// Gets or sets the contacts list.
        /// </summary>
        /// <value>The contacts list.</value>
        public ItemsControl ContactsList { get; set; }

        private ScrollViewer MainScrollViewerElement { get; set; }

        #endregion

        #region properties

        private ObservableCollection<WLCategoryContacts> contacts;

        /// <summary>
        /// Gets or sets the contacts of the user.
        /// </summary>
        /// <value>The contacts.</value>
        public ObservableCollection<WLCategoryContacts> Contacts
        {
            get { return contacts; }
            set
            {
                contacts = value;
                if (contacts.Count > 0)
                {
                    ContactsList.ItemsSource = Contacts;
                }
            }
        }

        #endregion

        #region dependency properties

        #endregion

        #region Methods

        private void SetScrolViewerHeight()
        {
            if (!double.IsNaN(this.Height) || this.Height == 0)
            {
                MainScrollViewerElement.Height = this.Height-10;
            }
            else
            {
                MainScrollViewerElement.Height = this.MinHeight-10;
            }
        }

        #endregion
    }
}
